package com.team.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.ContactDao;
import com.team.dao.RecordDao;
import com.team.dao.UserDao;
import com.team.entity.Record;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.StringUtil;
import com.team.util.TUtil;

public class RecordServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String method = request.getParameter("_m");
		
		if("find".equals(method)){
			findRecord(request,response);
		}else if("save".equals(method)){
			saveRecord(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else {
			response.sendError(404,"request a error path");
			return ;
		}
	}
	

	private void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String conId = request.getParameter("conId");
		int id = Integer.parseInt(conId);
		RecordDao rd = new RecordDao();
		System.out.println("conId"+id);
		List<Record> listRecord = rd.selectByRecordId(id);
		Map<Record,User> map = new HashMap<Record,User>();
		UserDao ud = new UserDao();
		if(listRecord != null){
			for(Record record : listRecord){
				User temp = ud.selectById(record.getUserId());
				if(temp != null){
					map.put(record, temp);
				}else {
					System.out.println("find record ~user fail");
				}
				
			}
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_RECORMAP	, map);
			session.setAttribute(Const.SESSION_RECORDS, listRecord);
			System.out.println("save record succ finally");
			response.sendRedirect("contactnote.jsp");
			return ;
		}else {
			System.out.println(id+"~record find error");
			
		}
		
	}
		

	private void saveRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String conId = request.getParameter("conId");
		int contactId = Integer.parseInt(conId);
		System.out.println("conId"+contactId);
		String content = request.getParameter("content");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		Record record = new Record();
		
		record.setContactId(contactId);
		record.setContent(content);
		record.setCreateTime(TUtil.getNow());
		record.setUserId(user.getId());
		
		
		if(StringUtil.isEmpty(conId)||StringUtil.isEmpty(content)){
			response.sendRedirect("contactnote.jsp?code=1001");
			return ;
		}else {
			
			RecordDao rd = new RecordDao();
			int num = rd.saveRecord(record);
			if(num > 0){
				System.out.println(" new record save succ");
				response.sendRedirect("record.jspx?_m=select&conId="+conId);
				return ;
			}else {
				
				System.out.println("record save fail ");
			}
		}
		
	}
	

	private void findRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		findOrSelectRecord(request);
		response.sendRedirect("contact.jsp");
		return ;
	}
	
	private void findOrSelectRecord(HttpServletRequest request){
		String conId = request.getParameter("conId");
		int id = Integer.parseInt(conId);
		RecordDao rd = new RecordDao();
		
		List<Record> listRecord = rd.selectByRecordId(id);
		Map<Record,User> map = new HashMap<Record,User>();
		UserDao ud = new UserDao();
		if(listRecord != null){
			for(Record record : listRecord){
				User temp = ud.selectById(record.getUserId());
				if(temp != null){
					map.put(record, temp);
				}else {
					System.out.println("find record ~user fail");
				}
				
			}
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_RECORMAP	, map);
			session.setAttribute(Const.SESSION_RECORDS, listRecord);
		}else {
			System.out.println(id+"~record find error");
			
		}
		
	}

}
